#include "CarStop.h"
#include <iostream>

std::string GetTimeStr(time_t time)
{
    tm *m = localtime(&time); 
    char s[1024] = {0};
    sprintf(s,"%d年%d月%d日  %d:%d:%d\n",m->tm_year + 1900,m->tm_mon + 1,m->tm_mday,m->tm_hour,m->tm_min,m->tm_sec);
    return std::string(s);
}

CarStop::~CarStop()
{
    while(stop.IsEmpty() == false)
    {
        Car *c = (Car*)*stop.GetTop();
        delete c;
        stop.Pop();
    }
    while(pavement.IsEmpty() == false)
    {
        Car *c = (Car*)*pavement.GetFront();
        delete c;
        pavement.Pop();
    }
}

void CarStop::ComeCar()
{
    Car *c = new Car("");
    std::cout << "请输入车牌号：";
    std::cin >> c->Name;
    c->arriveTime = time(nullptr);  // 获取当前时间
    std::cout<<"车辆|"<<c->Name<<"|在"<<GetTimeStr(c->arriveTime)<<"到达停车场"<<std::endl;

    if(stop.Size() == size)
    {
        std::cout << "停车场已满，"<< c->Name 
                << "停在便道上第"<<pavement.Size() + 1 <<"个位置"
                << std::endl;
        pavement.Push(c);
    }
    else
    {
        std::cout<<"停车场未停满，停入停车场第"<<stop.Size() + 1 <<"个位置"<<std::endl;
        stop.Push(c);
    }

    

}
void CarStop::LeaveCar()
{
    std::cout<<"请输入要离开的车牌号：";
    std::string LeaveCarName;
    std::cin >> LeaveCarName;

    LinkStack tmpStop;  //临时找
    while(stop.IsEmpty() == false)
    {
        Car *c = (Car*)*stop.GetTop();
        stop.Pop();
        if(c->Name == LeaveCarName)  //找到要离场的车
        {
            c->leaveTime = time(nullptr);  // 获取当前时间
            double money = 1.0 * (c->leaveTime - c->arriveTime) / 3600 * 200;
            std::cout<<"车辆|"<<c->Name<<"|在"
                    <<GetTimeStr(c->leaveTime)
                    <<"离开停车场，停车费用|"
                    <<money<<"|元"
                    <<std::endl;
            delete c;
            break;
        }
        tmpStop.Push(c);
    }
    while(tmpStop.IsEmpty() == false)
    {
        stop.Push(*tmpStop.GetTop());
        tmpStop.Pop();
    }

    //停车场有车位 且 便道上有车
    if(stop.Size() < size && pavement.IsEmpty() == false)
    {
        Car *c = (Car*)*pavement.GetFront();
        c->arriveTime = time(nullptr);  // 获取当前时间
        std::cout<<"便道上的车辆|"<<c->Name<<"在"<<GetTimeStr(c->arriveTime)<<"进入停车场"<<std::endl;
        pavement.Pop();
        stop.Push(c);
    }
}

void CarStop::Show()
{
    LinkStack tmpStop;  //临时栈
    while(stop.IsEmpty() == false)
    {
        tmpStop.Push(*stop.GetTop());
        stop.Pop();
    }

    while(tmpStop.IsEmpty() == false)
    {
        Car *c = (Car*)*tmpStop.GetTop();
        std::cout<<"🚗|"<<c->Name<<"|"<<std::endl;
        stop.Push(c);
        tmpStop.Pop();
    }

    LinkQueue tmpPavement;  //临时队列
    while(pavement.IsEmpty() == false)
    {
        tmpPavement.Push(*pavement.GetFront());
        pavement.Pop();
    }
    while(tmpPavement.IsEmpty() == false)
    {
        Car *c = (Car *)*tmpPavement.GetFront();
        std::cout << "      🚗|" << c->Name << "|" << " "<<std::endl;
        pavement.Push(c);
        tmpPavement.Pop();
    }
}
